package com.javatian.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.javatian.common.Result;
import com.javatian.entity.AdminInfo;
import com.javatian.service.AdminInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping(value = "/adminInfo")
public class AdminInfoController {

    @Resource
    private AdminInfoService adminInfoService;

    /**
     * 新增一个管理员
     */
    @PostMapping
    public Result<AdminInfo> add(@RequestBody AdminInfo adminInfo){
        adminInfoService.add(adminInfo);
        return Result.success(adminInfo);
    }

    /**
     * 根据主键id删除一个管理员
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        adminInfoService.delete(id);
        return Result.success();
    }

    /**
     * 修改一个管理员
     */
    @PutMapping
    public Result update(@RequestBody AdminInfo adminInfo){
        adminInfoService.update(adminInfo);
        return Result.success();
    }

    /**
     * 根据主键id获取一个管理员对象
     */
    @GetMapping("/{id}")
    public Result<AdminInfo> detail(@PathVariable Long id){
        AdminInfo adminInfo = adminInfoService.findById(id);
        return Result.success(adminInfo);
    }

    /**
     * 查询所有管理员记录
     */
    @GetMapping
    public Result<List<AdminInfo>> all(){
        return Result.success(adminInfoService.findAll());
    }

    /**
     * 根据条件分页查询
     * @param name 用户名
     * @param pageNum 第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @GetMapping("/page/{name}")
    public Result<PageInfo<AdminInfo>> page(@PathVariable String name,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "5") Integer pageSize){
        return Result.success(adminInfoService.findPage(name, pageNum, pageSize));
    }

    /**
     * 生成批量导入模版
     */
    @GetMapping("/getExcelModel")
    public void getExcelModel(HttpServletResponse response) throws IOException {
        //生产excel
        Map<String,Object> row = new LinkedHashMap<>();
        row.put("name","admin");
        row.put("password","123456");
        row.put("nickName","管理员");
        row.put("sex","男");
        row.put("age","22");
        row.put("birthday","2020-11-01 11:36:59");
        row.put("phone","15926611011");
        row.put("address","武汉学院");
        row.put("email","896408579@qq.com");
        row.put("cardId","421023200000000000");
        row.put("level",1);


        ArrayList<Map<String, Object>> list = CollUtil.newArrayList(row);

        //写excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list,true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=adminInfoModel.xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        //将Writer刷新到OutPut
        writer.flush(outputStream, true);
        outputStream.close();
        writer.close();
        IoUtil.close(System.out);

    }

    /**
     * 批量通过excel添加信息
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        List<AdminInfo> infoList = ExcelUtil.getReader(file.getInputStream()).readAll(AdminInfo.class);
        if (!CollectionUtil.isEmpty(infoList)){
            //处理掉姓名为空的数据
            List<AdminInfo> resultList = infoList.stream().filter(x -> ObjectUtil.isNotEmpty(x.getName())).collect(Collectors.toList());
            for (AdminInfo info:resultList) {
                adminInfoService.add(info);

            }

        }
        return Result.success();
    }





}
